package com.example.caimogu.fragmentAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.caimogu.bean.Goods;
import com.example.caimogu.businessapp.R;

import java.util.List;

/**
 * Created by Caimogu on 2016/4/21.
 */
public class MyStoreAdapter extends BaseAdapter{

    private List<Goods> goodsList;
    private LayoutInflater inflater;
    private Context context;
    private List<String> fileList;
    private List<String> fileListParent;


    public MyStoreAdapter(List<Goods> goodsList,List<String> fileListParent, LayoutInflater inflater,Context context,List<String> fileList) {
        this.fileListParent = fileListParent;
        this.goodsList = goodsList;
        this.inflater = inflater;
        this.context = context;
        this.fileList = fileList;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        //return goodsList.get(position).getId();
        return 1001;
    }

    @Override
    public View getView( final int position, View convertView, ViewGroup parent) {
        convertView =  inflater.inflate(R.layout.mystore_goodslist,null);

        ImageView snap = (ImageView) convertView.findViewById(R.id.iv_ms_snap_goods);
        TextView title=(TextView)convertView.findViewById(R.id.tv_ms_goods_name);
        TextView content=(TextView)convertView.findViewById(R.id.tv_ms_goods_info);
        Button btn_edit = (Button) convertView.findViewById(R.id.btn_ms_gl_edit);
        Button btn_del = (Button) convertView.findViewById(R.id.btn_ms_gl_delete);
        title.setText(goodsList.get(position).getName());
        content.setText("单价："+goodsList.get(position).getPrice()+"备注："+goodsList.get(position).getInformation());
        Bitmap bitmap = BitmapFactory.decodeFile(fileListParent.get(position));
        //picture.setImageBitmap(bitmap);
        snap.setImageBitmap(bitmap);
//        snap.setImageResource(goodsList.get(position).getPicPath());
//        snap.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.i("john", goodsList.get(position).getTitle());
//            }
//        });

        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, goodsList.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });
        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.i("caimogu","mystore...goodslist...btn_edit");
                Toast.makeText(context,"编辑",Toast.LENGTH_SHORT).show();
            }
        });
        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("caimogu", "mystore...goodslist...btn_del");

                goodsList.remove(position);
                notifyDataSetChanged();

            }
        });



        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
}
